/*
 * This file is part of Cadmium.
 * Copyright (C) 2007-2010 Xavier Clerc.
 *
 * Cadmium is free software; you can redistribute it and/or modify
 * it under the terms of the GNU Lesser General Public License as published by
 * the Free Software Foundation; either version 3 of the License, or
 * (at your option) any later version.
 *
 * Cadmium is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU Lesser General Public License for more details.
 *
 * You should have received a copy of the GNU Lesser General Public License
 * along with this program.  If not, see <http://www.gnu.org/licenses/>.
 */

package fr.x9c.cadmium.primitives.cadmiumjdbc;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.util.Arrays;

/**
 * This class represents the context associated with CadmiumJDBC-based modules
 * generated by <a href="http://nickel.x9c.fr">Nickel</a>.
 *
 * @author <a href="mailto:cadmium@x9c.fr">Xavier Clerc</a>
 * @version 1.1
 * @since 1.1
 */
public final class JDBCSlot {

    /** Database connection. */
    private final Connection connection;

    /** Prepared statements. */
    private final PreparedStatement[] statements;

    /**
     * Constructs a JDBC slot.
     * @param c associated connection - should not be <tt>null</tt>
     * @param ps prepared statements - should not be <tt>null</tt>
     */
    public JDBCSlot(final Connection c, final PreparedStatement[] ps) {
        assert c != null : "null c";
        assert ps != null : "null ps";
        this.connection = c;
        this.statements = Arrays.copyOf(ps, ps.length);
    } // end constructor(Connection, PreparedStatement[])

    /**
     * Returns the database connection.
     * @return the database connection
     */
    public Connection getConnection() {
        return this.connection;
    } // end method 'getConnection()'

    /**
     * Returns the number of prepared statements.
     * @return the number of prepared statements
     */
    public int getStatementCount() {
        return this.statements.length;
    } // end method 'getStatementCount()'

    /**
     * Returns the prepared statement at a given index.
     * @param i index of the prepared statement to return
     * @return the prepared statement at the passed index
     * @throws ArrayIndexOutOfBoundsException if the passed index is invalid
     */
    public PreparedStatement getStatement(final int i)
        throws ArrayIndexOutOfBoundsException {
        return this.statements[i];
    } // end method 'getStatement(int)'

    /**
     * Returns (a copy of) the prepared statements.
     * @return (a copy of) the prepared statements
     */
    public PreparedStatement[] getStatements() {
        return Arrays.copyOf(this.statements, this.statements.length);
    } // end method 'getStatements()'

} // end class 'JDBCSlot'
